/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using OpenServers.Net.Common.FileSystem;
using OpenServers.Net.Common.System;

namespace OpenServers.Net.Common.FileSystem.Virtual {
    /// <summary>
    /// A base file system object which simplifies creating <see cref="IFileSystemObject"/> objects.
    /// </summary>
    public abstract class VirtualFileSystemObject : IFileSystemObject {
        private readonly IDirectory _parent;
        private readonly string _name;
        private DateTime _lastModified;

        private readonly ISystem system = new DefaultSystem();
        private bool _exists = true;
        private bool _readOnly = false;

        /// <summary>
        /// constructor.
        /// </summary>
        public VirtualFileSystemObject(IDirectory parent, string name) {
            this._parent = parent;
            this._name = name;
            this._lastModified = system.DateTimeNow;
        }

        /// <summary>
        /// The name of the object.
        /// </summary>
        public virtual string Name {
            get {
                if (Parent == null) {
                    return "/";
                }
                return _name;
            }
        }

        /// <summary>
        /// The last modified date.
        /// </summary>
        public virtual DateTime LastModified {
            get { return _lastModified; }
            set { _lastModified = value; }
        }

        /// <summary>
        /// Deletes the object.
        /// </summary>
        public virtual void Delete() {
            if (Parent is VirtualDirectory) {
                VirtualDirectory parentDir = (VirtualDirectory)Parent;
                parentDir.GetItems().Remove(this);
            } else {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// flag whether the object exists.
        /// </summary>
        public virtual bool Exists {
            get { return _exists; }
            set { _exists = value; }
        }

        /// <summary>
        /// The parent object.
        /// </summary>
        public virtual IDirectory Parent {
            get { return _parent; }
        }

        /// <summary>
        /// flag marking this object as read only.
        /// </summary>
        public bool ReadOnly {
            get { return _readOnly; }
            set { _readOnly = value; }
        }

        /// <summary>
        /// moves the object to another location.
        /// </summary>
        /// <param name="fso"></param>
        public virtual void MoveTo(IFileSystemObject fso) {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the full path of this object.
        /// </summary>
        public string FullPath {
            get { return FileSystemUtils.GetFullPath(this); }
        }
    }
}
